package org.springframework.web.servlet;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SavaUserServlet extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取用户信息，表单提交的，这些数据被自动封装到request对象中
		//从request中获取这些信息
		//表单数据是这样的格式提交的，POST请求，在请求中提交，数据格式：
		//username=admin&password=123&sex=m&interest=sport&interest=music&grade=dz&introduce=ok
		/**
		 * Map<String,String[]>
		 * key                     value
		 * ------------------------------------------------
		 * username             {"admin"}
		 * pass                 {"123"}
		 * sex                  {"m"}
		 * interest             {"sport"，"music"}
		 * grade                {"dz"}
		 * introduce            {"ok"}
		 */
		 String username = request.getParameter("username");
		 String password = request.getParameter("password");
		 String sex = request.getParameter("sex");
		 String interest = request.getParameter("interest");
		 String grade = request.getParameter("grade");
		 String introduce = request.getParameter("introduce");
		 
		 System.out.println(username);
		 System.out.println(password);
		 System.out.println(sex);
		 System.out.println(interest);
		 System.out.println(grade);
		 System.out.println(introduce);
		 
		 //获取所有兴趣爱好
		 //这个方法适合取checkbox复选中的数据
		String[] interests = request.getParameterValues("interest");
		for(String in: interests){
			System.out.println(in);
		}
		
//		//获取整个参数map集合
//		Map<String,String[]> parameterMap = request.getParameterMap();
//		Set<String> names = parameterMap.keySet();
//		for(String name: names){
//			String[] values = parameterMap.get(name);
//			System.out.println(name + "=" +values);
//		}
//		admin
//		123
//		m
//		sport
//		dz
//		ok
//		sport
//		username=[Ljava.lang.String;@544f4ab4
//		password=[Ljava.lang.String;@21b19aaa
//		sex=[Ljava.lang.String;@1321202e
//		interest=[Ljava.lang.String;@63c7b5d1
//		grade=[Ljava.lang.String;@4c71df84
//		introduce=[Ljava.lang.String;@48097a51
		
		Map<String,String[]> parameterMap = request.getParameterMap();
		Set<String> names = parameterMap.keySet();
		for(String name: names){
			String[] values = parameterMap.get(name);
			System.out.println(name + "=");
			for(String value: values) {
				System.out.print(value + "");
			}
			System.out.println();
		}
//		admin
//		123
//		m
//		sport
//		dz
//		ok
//		sport
//		username=
//		admin
//		password=
//		123
//		sex=
//		m
//		interest=
//		sport
//		grade=
//		dz
//		introduce=
//		ok
		
		Enumeration<String> keys =request.getParameterNames();
		while(keys.hasMoreElements()){
			String name = keys.nextElement();
			System.out.println(name);
		}
//		admin
//		123
//		m
//		sport
//		dz
//		ok
//		sport
//		username=
//		admin
//		password=
//		123
//		sex=
//		m
//		interest=
//		sport
//		grade=
//		dz
//		introduce=
//		ok
//		username
//		password
//		sex
//		interest
//		grade
//		introduce
		
	}

}
